package com.example.spring_java_oj.Util;

import com.example.spring_java_oj.model.UserInfo;
import com.example.spring_java_oj.service.userInfo.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LoginUtil {

    public static boolean login(HttpServletRequest request, UserInfoService service){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        UserInfo userInfo = service.queryByName(username);
        if (userInfo!=null && userInfo.getId()>=0){
            return true;
        }
        return false;
    }

    //判断验证码是否正确
    public static boolean isTrue(String captcha, HttpServletRequest request, StringRedisTemplate stringRedisTemplate){
        //从session中获取真正的验证码
        //HttpSession session = request.getSession();
        //String realCaptcha = (String) session.getAttribute("verifyCode");
        //从redis中获取验证码
        String realCaptcha = stringRedisTemplate.opsForValue().get("verifyCode");
        if(captcha != null && realCaptcha != null && realCaptcha.equalsIgnoreCase(captcha)){
            return true;
        }else {
            return false;
        }
    }
}
